package com.taoyibao.hongcanting.ui.mine.fragment;

import android.os.Bundle;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.fragment.BaseListFragment;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.WalletDetailModel;
import com.taoyibao.hongcanting.model.WalletDetailPageModel;
import com.taoyibao.hongcanting.ui.mine.views.ItemWalletDetailsArea;
import com.zhy.adapter.recyclerview.base.ViewHolder;

/**
 * @author TangAnna
 * @description: 钱包明细
 * @date :${DATA} 16:55
 */
public class WalletDetailsFragment extends BaseListFragment<WalletDetailModel> {

    /**
     * @param status 明细类型
     * @return
     */
    public static WalletDetailsFragment getInstance(int status) {
        Bundle bundle = new Bundle();
        bundle.putInt(CodeConstant.STATUS, status);
        WalletDetailsFragment fragment = new WalletDetailsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void bindItemData(ViewHolder holder, WalletDetailModel walletDetailModel, int position) {
        ItemWalletDetailsArea itemWalletDetailsArea = holder.getView(R.id.layout_item_walletDetail);
        itemWalletDetailsArea.setData(walletDetailModel);
    }

    @Override
    public int bindItemView() {
        return R.layout.item_wallet_details_shell;
    }

    @Override
    public void loadData() {
        int type = getArguments().getInt(CodeConstant.STATUS, -1);
        JsonApiWrapper.getApiService().userWalletDetails(SpUtils.getToken(), type, mCurrentPage, mPageSize)
                .compose(new DefaultTransformer<CompanyBaseModel<WalletDetailPageModel>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<WalletDetailPageModel>>(getContext(), true) {
                    @Override
                    public void onNext(CompanyBaseModel<WalletDetailPageModel> result) {
                        if (result.code == NetCode.HTTP_200) {
                            addData(result.data.orders_info);
                            mTotalPage = result.data.page_info.total_page;
                        } else {
                            finishRefreshAndLoadMore();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        finishRefreshAndLoadMore();
                    }
                });
    }
}
